package com.zy.fcs.service;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zy.fcs.bean.GetMedia;
import com.zy.fcs.bean.param.AfterUploadParam;
import com.zy.fcs.bean.param.GetMediaUrlsParam;
import com.zy.fcs.domain.TblMediaManagement;
import com.zy.fcs.repository.TblMediaManagementRepository;
import com.zy.fcs.repository.TblPcmDoccdeRepository;
import com.zy.fcs.repository.TblPcmDoctypRepository;
import com.zy.fcs.service.oss.OssService;

/**
 * 树形结构操作
 *
 * @author zhanghengchang
 * @date 2018/3/6
 */
@Service
public class MediaManagementService {

    private static Logger logger = LoggerFactory.getLogger(MediaManagementService.class);

    @Autowired
    TblMediaManagementRepository tblMediaManagementRepository;

    @Autowired
    TblPcmDoctypRepository tblPcmDoctypRepository;

    @Autowired
    TblPcmDoccdeRepository tblPcmDoccdeRepository;

    @Autowired
    OssService ossService;

    /**
     * 文件上传后的处理
     *
     * @param param 上传后处理参数
     * @return 成功消息
     */
    public void afterUpload(AfterUploadParam param) throws Exception {
        TblMediaManagement media = tblMediaManagementRepository.findByInstNoAndOrderNoAndMediaUrl(param.getInstNo(),
            param.getOrderNo(), param.getMediaUrl());

        if (media == null) {
            // 新建记录
            media = new TblMediaManagement();
            BeanUtils.copyProperties(param, media);

            String category1Desc = tblPcmDoctypRepository
                .findByDtInstNoAndDtDocType(param.getInstNo(), param.getCategory1()).getDtDesc();
            String category2Desc = tblPcmDoccdeRepository
                .findByDcInstNoAndDcDocTypeAndDcDocCd(param.getInstNo(), param.getCategory1(), param.getCategory2())
                .getDcDocDescription();
            media.setCategory1Desc(category1Desc);
            media.setCategory2Desc(category2Desc);
            media.setUpdateId(param.getCreateId());
        } else {
            // 更新记录
            media.setUpdateId(param.getCreateId());
        }

        tblMediaManagementRepository.save(media);

        return;
    }

    /**
     * 删除影像文件（只删除数据，不删除oss文件）
     *
     * @param id 主键
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteMedia(Long id) throws Exception {
        tblMediaManagementRepository.delete(id);
    }

    /**
     * 获得订单下所有影像文件路径
     *
     * @param param 机构号，订单号
     * @return 订单下所有影像文件路径
     */
    public List<GetMedia> getMediaUrls(GetMediaUrlsParam param) throws Exception {
        List<TblMediaManagement> list
            = tblMediaManagementRepository.findByInstNoAndOrderNo(param.getInstNo(), param.getOrderNo());
        List<GetMedia> listUrl = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            GetMedia getMedia = new GetMedia();
            BeanUtils.copyProperties(list.get(i), getMedia);
            getMedia.setUrl(ossService.getPreSignedUrl(list.get(i).getMediaUrl()));
            listUrl.add(getMedia);
        }
        return listUrl;
    }
}
